package com.media.common.mail;

import java.util.List;

/**
 * 邮箱工具类
 */
public class IEmailTool {

    /**
     * description ：Html形式邮箱内容
     *
     * @param htmlContent 标题内容
     * @param contentList 表格内容
     * @return String
     */
    public static String generateHtml(String htmlContent, List<String[]> contentList) {
        StringBuilder stb = new StringBuilder();
        stb.append("<!DOCTYPE html>\n")
                .append("<html lang=\"zh\">\n")
                .append(generateHtmlBody(htmlContent, contentList))
                .append("</html>");
        return stb.toString();
    }

    /**
     * description ：控制表格内容显示
     *
     * @param htmlContent 标题内容
     * @param contentList 表格内容
     * @return String
     */
    public static String generateHtmlBody(String htmlContent, List<String[]> contentList) {
        StringBuilder stb = new StringBuilder();
        stb.append("<body>\n")
                .append("<div style=\"font-size: 16px;\">" + htmlContent + "</div>")
                .append("<table style=\"border-width: 1px;border-collapse: collapse;margin: 0 auto;width: 100%;\">\n");
        for (int i = 0; i < contentList.size(); i++) {
            if (i == 0) {
                stb.append("    <tr>\n");
                String[] tableHead = contentList.get(0);
                for (int j = 0; j < tableHead.length; j++) {
                    stb.append("        <th style=\"border-width:1px;padding:8px;border-style:solid;background-color:#dedede;\">").append(tableHead[j]).append("</th>\n");
                }
                stb.append("    </tr>\n");
            } else {
                stb.append("    <tr>\n");
                String[] tableData = contentList.get(i);
                for (int j = 0; j < tableData.length; j++) {
                    stb.append("        <td style=\"border-width:1px;padding:8px;border-style:solid;\">").append(tableData[j]).append("</td>\n");
                }
                stb.append("    </tr>\n");
            }
        }
        stb.append("</table>\n")
                .append("</body>");
        return stb.toString();
    }

}
